import { request } from '@/utils/request'
import { formatDate } from '@/utils/date.js'
import  BASE_URL  from '@/utils/env.js'

// ========== 认证相关 ==========
export const authLogin = (data) => {
	return request({
		url: 'system/auth/login',
		method: 'POST',
		hideLoading: true,
		data
	})
}

// http://192.168.1.31:48080/system/user/getMyData
export const getMyData = () => {
	return request({
		url: 'system/user/getMyData',
		method: 'GET',
		hideLoading: true
	})
}

export const GETUserInfo = () => {
	return request({
		url: 'system/auth/get-user-info',
		method: 'GET',
		hideLoading: true
	})
}

// ========== 未读站内信 ==========
export const GETUnreadNotifyList = (params) => {
	return request({
		url: 'system/notify-message/get-unread-list',
		method: 'GET',
		params
	})
}

// 新增：获取各类未读数量（OA审批/预警/通知）
export const GETNotifyMyWdNum = () => {
	return request({
		url: 'system/notify-message/my-WdNum',
		method: 'GET',
		hideLoading: true
	})
}

// ========== 站内信 ==========
export const putUpdateSetRead = (params) => {
	return request({
		url: 'system/notify-message/update-read-one',
		method: 'GET',
		// hideLoading: true,
		params
	})
}



// ========== 站内信 ==========
export const getZhanXinForPage = (params) => {
	return request({
		url: 'system/notify-message/my-page',
		method: 'GET',
		params
	})
}

// ========== 社交绑定 ==========
export const bindSocialUser = (data) => {
	return request({
		url: 'system/social-user/bind',
		method: 'POST',
		data
	})
}

// ========== 动销员 - 门店 & 打卡 ==========
export const GETNearestShop = (data) => {
	return request({
		url: 'system/dx-shop/getNearestShop',
		method: 'POST',
		data
	})
}

export const createShopCheck = (data) => {
	return request({
		url: 'system/dx-shop-check/create',
		method: 'POST',
		data
	})
}

export const GETBandShop = (data) => {
	return request({
		url: 'system/dx-shop/getBandShop',
		method: 'POST',
		data
	})
}

// 获取门店详情
export const GETShopDetail = (params) => {
	return request({
		url: 'system/dx-shop/getShopDetail',
		method: 'GET',
		params
	})
}

// 获取我的门店列表
export const GETMyShops = () => {
	return request({
		url: 'system/dx-shop/listMyShops',
		method: 'GET',
		hideLoading: true
	})
}

export const GETUncheckedStore = (params) => {
	return request({
		url: 'system/dx-shop/getUncheckedStore',
		method: 'GET',
		params
	})
}

// ========== 培训中心 ==========
export const GETTrainingList = (params) => {
	return request({
		url: 'system/dx-curses/page',
		method: 'GET',
		params
	})
}

export const GETTrainingDetail = (params) => {
	return request({
		url: 'system/dx-curses/get',
		method: 'GET',
		params
	})
}

export const likeTraining = (data) => {
	return request({
		url: 'system/dx-curses-like/create',
		method: 'POST',
		data
	})
}

export const createTrainingComment = (data) => {
	return request({
		url: 'system/dx-curses-comment/create',
		method: 'POST',
		data
	})
}

export const submitTrainingHomework = (data) => {
	return request({
		url: 'system/dx-curses-submit/create',
		method: 'POST',
		data
	})
}

export const GETTrainingSubmitDetail = (params) => {
	return request({
		url: 'system/dx-curses-submit/get',
		method: 'GET',
		params
	})
}

// ========== 文件上传 ==========
export const uploadFile = (data) => {
	// 注意：multipart/form-data 需要传 FormData 实例
	return request({
		url: BASE_URL + 'infra/file/upload',
		method: 'POST',
		headers: { 'Content-Type': 'multipart/form-data' },
		data
	})
}

// 图片上传（使用uni.uploadFile）
export const uploadImage = (filePath, fileType = 'checkin') => {
	console.log(BASE_URL + 'infra/file/upload');

	return new Promise((resolve, reject) => {
		const rawToken = uni.getStorageSync('token') || '';
		const bearerToken = rawToken ? (String(rawToken).startsWith('Bearer ') ? String(rawToken) : `Bearer ${String(rawToken)}`) : '';
		uni.uploadFile({
			url: BASE_URL + 'infra/file/upload',
			filePath: filePath,
			name: 'file',
			formData: {
				type: fileType
			},
			header: {
				'Authorization': bearerToken
			},
			success: (uploadFileRes) => {
				try {
					const data = JSON.parse(uploadFileRes.data)
					if (uploadFileRes.statusCode === 200 && data.code === 0) {
						// 接口返回的data是直接的URL字符串，封装成对象返回
						resolve({ url: data.data })
					} else {
						reject(new Error(data.message || '上传失败'))
					}
				} catch (e) {
					reject(new Error('解析响应失败'))
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

// ========== 用户设置 ==========
export const verifyGesturePassword = (params) => {
	return request({
		url: 'system/user/verifyGesturePassword',
		method: 'GET',
		params
	})
}

export const getMyShops = (params) => {
	params.dateStr = formatDate(params.dateStr);
	return request({
		url: 'system/dx-shop/getMyShops',
		method: 'GET',
		params 

	})
}


export const updateGuestPassword = (params) => {
	return request({
		url: 'system/user/update-guestPassword',
		method: 'GET',
		params  // 自动处理 null/undefined → {}

	})
}

export const updateUserPassword = (params) => {
	return request({
		url: 'system/user/update-password',
		method: 'PUT',
		hideLoading: true,
		 data: params
	})
}

// ========== 数据汇总 ==========
export const GETUserSummary = () => {
	return request({
		url: 'system/user/getSummary',
		method: 'GET'
	})
}

export const GETTodayCheckDetail = (params) => {
	return request({
		url: 'system/user/getTodayCheckDetail',
		method: 'GET',
		params
	})
}

export const GETNotExecutedShops = (params) => {
	return request({
		url: 'system/user/getNotExecuted',
		method: 'GET',
		params
	})
}

// ========== 打卡统计分页接口 ==========
export const GETShopCheckPage = (params) => {
	return request({
		url: 'system/dx-shop-check/page',
		method: 'GET',
		params
	})
}

// ========== 任务代办 ==========
export const GETTaskAgencyList = (params) => {
	return request({
		url: 'system/dx-task-feedback/getTaskAgency',
		method: 'GET',
		params
	})
}

// ========== 销售看板 ==========
export const GETSalesBoard = (params) => {
	return request({
		url: 'system/dx-shop/getSalesBoard',
		method: 'GET',
		params
	})
}

// ========== 薪资列表 ==========
export const listSalary = (params) => {
	return request({
		url: 'system/dx-salary/listSalary',
		method: 'GET',
		params
	})
}

// 打分接口
export const makeScoreOperation = (params) => {
	return request({
		url: 'system/dx-salary/makeScoreOperation',
		method: 'GET',
		params
	})
}

// ========== OA审批 ==========
// 待处理
export const GETTaskTodoPage = (params) => {
	return request({
		url: 'bpm/task/todo-page',
		method: 'GET',
		params
	})
}

// 我发起的
export const GETProcessInstanceMyPage = (params) => {
	return request({
		url: 'bpm/process-instance/my-page',
		method: 'GET',
		params
	})
}

// 已处理
export const GETTaskDonePage = (params) => {
	return request({
		url: 'bpm/task/done-page',
		method: 'GET',
		params
	})
}

// 抄送我的
export const GETProcessInstanceCopyPage = (params) => {
	return request({
		url: 'bpm/process-instance/copy/page',
		method: 'GET',
		params
	})
}

// 流程定义列表
export const GETProcessDefinitionList = (params) => {
	// 确保传递suspensionState为1的参数
	const newParams = { ...params, suspensionState: 1 };
	return request({
		url: 'bpm/process-definition/list',
		method: 'GET',
		params: newParams
	})
}

// 获取流程定义详情
export const GETProcessDefinitionGet = (params) => {
	// 确保固定传递suspensionState为1的参数
	const newParams = { ...params, suspensionState: 1 };
	return request({
		url: 'bpm/process-definition/get',
		method: 'GET',
		params: newParams
	})
}

// 获取OA数量统计
export const GETUserOANumber = () => {
	return request({
		url: 'bpm/task/getUserOANumber',
		method: 'GET',
		hideLoading: true
	})
}

// 创建流程实例
export const POSTProcessInstanceCreate = (data) => {
	return request({
		url: 'bpm/process-instance/create',
		method: 'POST',
		data: data
	})
}

// 获取审批详情
export const GETApprovalDetail = (params) => {
	return request({
		url: 'bpm/process-instance/get-approval-detail',
		method: 'GET',
		params: params
	})
}

// 审批通过
export const PUTTaskApprove = (data) => {
	return request({
		url: 'bpm/task/approve',
		method: 'PUT',
		data: data
	})
}

// 审批不通过
export const PUTTaskReject = (data) => {
	return request({
		url: 'bpm/task/reject',
		method: 'PUT',
		data: data
	})
}
// ========== 绩效考核 ==========
export const GETPerformanceAssessment = (params) => {
	return request({
		url: 'system/dx-salary/page',
		method: 'GET',
		params
	})
}

// 获取我的动销员
export const getMyMobilizers = (params) => {
	return request({
		url: 'system/user/getMyMobilizers',
		method: 'GET',
		params
	})
}

// 获取动销员详情
export const getMobilizersDetail = (params) => {
	return request({
		url: 'system/user/getMobilizersDetail',
		method: 'GET',
		params
	})
}

// 获取动销员销售和物料领取详情
export const getMobilizersSaleAndMaterialsClaimCursesSubmitDetail = (params) => {
	return request({
		url: 'system/user/getMobilizersSaleAndMaterialsClaimCursesSubmitDetail',
		method: 'GET',
		params
	})
}

// ========== 物料领取 ==========
export const GETMyMaterialsClaim = (params) => {
	return request({
		url: 'system/dx-materials-claim/getMyMaterialsClaim',
		method: 'GET',
		params
	})
}

// 获取物料详情
export const GETMaterialDetail = (params) => {
	return request({
		url: 'system/dx-materials-claim/get',
		method: 'GET',
		params
	})
}

// 物料投放
export const CREATE_MATERIAL_DELIVERY = (data) => {
	return request({
		url: 'system/dx-materials-delivery/create',
		method: 'POST',
		data
	})
}

// 确认收货
export const CONFIRM_MATERIAL_ORDER = (params) => {
	return request({
		url: 'system/dx-materials-claim/confirmOrder',
		method: 'GET',
		params
	})
}

// 新增：获取物料基础信息（包含库存 stock、已领取 ylSum）
export const GETMaterialsBaseInfo = (params) => {
	return request({
		url: 'system/dx-materials/get',
		method: 'GET',
		params
	})
}

// 新增：获取全部物料列表（用于下拉选项），包含 stock、ylSum
export const GETMaterialsAllList = (params = {}) => {
	return request({
		url: 'system/dx-materials/getAllList',
		method: 'GET',
		params
	})
}